<?php $emacs=0;

/*
 * Copyright 2006-2009 Jeffrey Palm
 */

/* 
 * This is the javascript that creates all the markers for the google
 * map.  You need to include map.js along with this file.  
 *
 * Example:
 *
 * <html>
 *   <head>
 *     <script type="text/javascript" src="map.js"></script>
 *     <script type="text/javascript" src="map.js.php"></script>
 *   </head>
 *   <body onload="onLoad()">
 *     <div id="map" style="width:100%; height:100%"></div>
 *   </body>
 * </html>
 * 
 */

include 'common.php';
include 'geo.php';

header('Content-type: text/javascript');

// If we specify an id, use that id
$names = array();
if (isset($_REQUEST['id']) && $_REQUEST['id']) {
  $names[]= $_REQUEST['id'];
 } else {
  getPhotoNames(-1);
 }

function n($s) {
  echo $s;
  echo "\n";
}

// Create the function to create all the markers
n('function createMarkers() {');
$latSum = 0;
$lngSum = 0;
$numNamesUsed = 0;
foreach ($names as $name) {
  $image = fullImageName($name);
  $geo = new GeoInformation($image);
  if ($geo->isValid()) {
    $des = getDescription($name);
    $loc = getLocation($name);
    $lat = $geo->getLat();
    $lng = $geo->getLng();
    $latSum += $lat;
    $lngSum += $lng;
    $numNamesUsed += 1;
    n('  createMarker("' . $name . '","' . addslashes($des) . '","' . addslashes($loc) . '",' . $lat . ',' . $lng . ');');
  }
}
n('}');

// Set avgLat and avgLng so we know where to center the map
// We'll allow it to blow up if $numNamesUsed is 0, but this won't
// come through in Javascript world (trust me, it just happened, so
// intentionally for an alert
if ($numNamesUsed == 0) {
  n('alert("No locations were found")');
 }
n('avgLat = ' . ($latSum/$numNamesUsed) . ';');
n('avgLng = ' . ($lngSum/$numNamesUsed) . ';');

?>

